﻿using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace AppMuaSachNhanh
{
    public partial class _01_BestSeller : Form
    {
        private int viTriSo = 1;
        private string sqlConn = @"Data Source=DBAO-PC\SRVDBAO;Initial Catalog=MuaSachNhanh;User Id=sa;Password=123456;";

        public _01_BestSeller()
        {
            InitializeComponent();
        }

        #region Events
        private void _01_BestSeller_Load(object sender, EventArgs e)
        {
            this.txtViTriSo.Text = this.viTriSo.ToString();
            LoadData();
            this.txtTuaSach.Focus();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = new SqlConnection(sqlConn);
                string sqlInsert = "INSERT INTO BestSeller(TuaSach,TacGia,NXB,GiaBia,Nam,ViTri,Webisite,GiamGia)" +
                                   "VALUES(@TuaSach, @TacGia, @NXB, @GiaBia, @Nam, @ViTri, @Webisite, @GiamGia) ";
                conn.Open();

                SqlCommand cmd = new SqlCommand(sqlInsert, conn);
                cmd.Parameters.Add("@TuaSach", UpperFirstLetter(this.txtTuaSach.Text.Trim()));
                cmd.Parameters.Add("@TacGia", UpperFirstLetter(this.txtTacGia.Text.Trim()));
                cmd.Parameters.Add("@NXB", UpperFirstLetter(this.txtNXB.Text.Trim()));
                cmd.Parameters.Add("@GiaBia", this.txtGiaBia.Text.Trim());
                cmd.Parameters.Add("@Nam", this.txtNam.Text.Trim());
                cmd.Parameters.Add("@ViTri", this.txtViTriSo.Text.Trim());
                cmd.Parameters.Add("@Webisite", this.txtWebsite.Text.Trim());
                cmd.Parameters.Add("@GiamGia", this.txtGiamGia.Text.Trim());

                int result = cmd.ExecuteNonQuery();

                conn.Close();
                if (result > 0)
                {
                    MessageBox.Show("Insert Successful!");
                }
                //Load lai gia tri
                if (!this.txtViTriSo.Text.Trim().Equals(string.Empty))
                {
                    this.viTriSo = Convert.ToInt32(this.txtViTriSo.Text.Trim());
                    this.viTriSo++;
                }
                this.txtViTriSo.Text = this.viTriSo.ToString();
                this.txtTuaSach.Clear();
                this.txtTacGia.Clear();
                this.txtGiaBia.Clear();
                this.txtNXB.Clear();
                this.txtGiaBia.Clear();
                this.txtGiamGia.Clear();
                this.txtTuaSach.Focus();
                LoadData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
             try
            {
                string sqlDelete = "DELETE BestSeller WHERE ID=@ID";
                SqlConnection conn = new SqlConnection(sqlConn);
                
                SqlCommand cmd = new SqlCommand(sqlDelete, conn);
                cmd.Parameters.Add("@ID", this.txtID.Text);
                conn.Open();
                int result = cmd.ExecuteNonQuery();
                conn.Close();
                if (result > 0)
                {
                    MessageBox.Show("Delete Successful!");
                }
            }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
             }
        }

        private void btnRefesh_Click(object sender, EventArgs e)
        {
            if (!this.txtViTriSo.Text.Trim().Equals(string.Empty))
            {
                this.viTriSo = Convert.ToInt32(this.txtViTriSo.Text.Trim());
            }
            LoadData();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            this.txtTuaSach.Clear();
            this.txtTacGia.Clear();
            this.txtGiaBia.Clear();
            this.txtNXB.Clear();
            this.txtTuaSach.Focus();
            this.txtID.Clear();
            this.txtGiaBia.Clear();
        }

        private void gvBestSeller_SelectionChanged(object sender, EventArgs e)
        {

        }

        private void gvBestSeller_MouseUp(object sender, MouseEventArgs e)
        {
            DataGridView.HitTestInfo hti = this.gvBestSeller.HitTest(e.X, e.Y);
            if (hti.Type == DataGridViewHitTestType.Cell || hti.Type == DataGridViewHitTestType.RowHeader)
            {
                this.gvBestSeller.ClearSelection();
                this.gvBestSeller.Rows[hti.RowIndex].Selected = true;

                this.txtTuaSach.Text = this.gvBestSeller.SelectedCells[2].Value.ToString();
                this.txtTacGia.Text = this.gvBestSeller.SelectedCells[3].Value.ToString();
                this.txtNXB.Text = this.gvBestSeller.SelectedCells[4].Value.ToString();
                this.txtGiaBia.Text = this.gvBestSeller.SelectedCells[5].Value.ToString();
                this.txtGiamGia.Text = this.gvBestSeller.SelectedCells[6].Value.ToString();
                this.txtNam.Text = this.gvBestSeller.SelectedCells[0].Value.ToString();
                this.txtViTriSo.Text = this.gvBestSeller.SelectedCells[1].Value.ToString();
                this.txtWebsite.Text = this.gvBestSeller.SelectedCells[7].Value.ToString();
                this.txtID.Text = this.gvBestSeller.SelectedCells[8].Value.ToString();
            }
        }

        private void _01_BestSeller_KeyDown(object sender, KeyEventArgs e)
        {
            //Insert
            if (e.KeyCode == Keys.F5)
            {
                btnInsert_Click(null, null);
            }
            //Update
            if (e.KeyCode == Keys.F6)
            {
            }
            //Delete
            if (e.KeyCode == Keys.F6)
            {
            }
        }
        #endregion

        #region Methods
        private string UpperFirstLetter(string input)
        {
            StringBuilder sbOutput = new StringBuilder();
            string[] arrStr = input.Split(' ');
            string word = string.Empty;
            for (int i = 0; i < arrStr.Length; i++)
            {
                for (int j = 0; j < arrStr[i].Length; j++)
                {
                    if (j == 0)
                    {
                        word = char.ToUpper(arrStr[i][j]).ToString();
                    }
                    else
                    {
                        word += arrStr[i][j].ToString();
                    }
                }
                sbOutput.Append(word);
                sbOutput.Append(" ");
            }
            return sbOutput.ToString().Trim();

        }

        private void LoadData()
        {
            try
            {
                SqlConnection conn = new SqlConnection(sqlConn);
                string sql = "SELECT Nam,ViTri,TuaSach,TacGia,NXB,GiaBia,GiamGia,Webisite,ID FROM BestSeller ORDER BY NgayTao DESC";
                DataSet ds = new DataSet();
                conn.Open();
                SqlDataAdapter adSelect = new SqlDataAdapter(sql, conn);
                adSelect.Fill(ds);
                conn.Close();
                this.gvBestSeller.DataSource = ds.Tables[0];

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
}
